using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_IdNumber : System.Web.UI.UserControl
{
    protected string strIdPrefix = "";
    protected string strIdPostfix = "";
    protected string strIdNumber = "";
    protected string strValidatinoGroup = "";
    protected string strClientScript = "";
    protected string strPrefixErrorMessage = "Choose ID number initial character.";
    protected string strIdNoErrorMessage = "ID number is not valid.";
    protected string strIdNoLengthErrorMessage = "ID number must be 13 digit.";
    protected string strIdNoRequireErrrorMessage = "Require ID number.";
    protected string strPostfixErrorMessage = "Choose ID number last character.";
    protected bool bEnabled = true;
    protected bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtIdNumber.Attributes.Add("onblur", "ShowValidationEffectThree('" + txtIdNumber.ClientID + "','" + rfvIdNumber.ClientID + "','" + csvIdNumber.ClientID + "','" + revIdNumberLength.ClientID + "');");
        }
    }

    public void ChangeValidateEffectPrefix(bool bChange)
    {
        //if (bChange)
        //{
        //    ddlIdPrefix.ControlStyle.CssClass = "validationError";
        //}
        //else
        //{
        //    ddlIdPrefix.ControlStyle.CssClass = "";
        //}
        //ddlIdPrefix.ControlStyle.CssClass = "";
    }

    public void ChangeValidateEffect(bool bChange)
    {
        if (bChange)
        {
            txtIdNumber.ControlStyle.CssClass = "validationError";            
        }
        else
        {
            txtIdNumber.ControlStyle.CssClass = "";            
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        #region "This region is for Thai"

            //ddlIdPrefix.ControlStyle.CssClass = "";            
            //ddlIdPostfix.ControlStyle.CssClass = "";
            if (revIdNumberLength.Enabled && revIdNumberLength.ValidationGroup == strValidationGroup)
            {
                if (rfvIdNumber.IsValid)
                {
                    revIdNumberLength.Validate();
                    if (revIdNumberLength.IsValid == false)
                    {
                        txtIdNumber.ControlStyle.CssClass = "validationError";
                    }
                    else
                    {
                        txtIdNumber.ControlStyle.CssClass = "";
                    }
                }
                else
                {
                    txtIdNumber.ControlStyle.CssClass = "validationError";
                }
            }
            else
            {
                txtIdNumber.ControlStyle.CssClass = "";
            }

            //if (csvIdNumber.Enabled && csvIdNumber.ValidationGroup == strValidationGroup)
            //{
            //    if (rfvIdNumber.IsValid && revIdNumberLength.IsValid)
            //    {
            //        csvIdNumber.Validate();
            //        if (csvIdNumber.IsValid == false)
            //        {
            //            txtIdNumber.ControlStyle.CssClass = "validationError";
            //        }
            //        else
            //        {
            //            txtIdNumber.ControlStyle.CssClass = "";
            //        }
            //    }
            //}
            //else
            //{
            //    txtIdNumber.ControlStyle.CssClass = "";
            //}

        #endregion

        #region "This region is for singapore"

        //if (csvIdPrefix.Enabled && csvIdPrefix.ValidationGroup == strValidationGroup)
        //{
        //    csvIdPrefix.Validate();
        //    if (csvIdPrefix.IsValid == false)
        //    {
        //        ddlIdPrefix.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        ddlIdPrefix.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    ddlIdPrefix.ControlStyle.CssClass = "";
        //}

        //if (rfvIdNumber.Enabled && rfvIdNumber.ValidationGroup == strValidationGroup)
        //{
        //    rfvIdNumber.Validate();
        //    if (rfvIdNumber.IsValid == false)
        //    {
        //        txtIdNumber.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        txtIdNumber.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    txtIdNumber.ControlStyle.CssClass = "";
        //}



        //if (csvIdNumber.Enabled && csvIdNumber.ValidationGroup == strValidationGroup)
        //{
        //    if (rfvIdNumber.IsValid && revIdNumberLength.IsValid)
        //    {
        //        csvIdNumber.Validate();
        //        if (csvIdNumber.IsValid == false)
        //        {
        //            txtIdNumber.ControlStyle.CssClass = "validationError";
        //        }
        //        else
        //        {
        //            txtIdNumber.ControlStyle.CssClass = "";
        //        }
        //    }
        //}
        //else
        //{
        //    txtIdNumber.ControlStyle.CssClass = "";
        //}

        //if (csvIdPostfix.Enabled && csvIdPostfix.ValidationGroup == strValidationGroup)
        //{
        //    if (csvIdPrefix.IsValid && csvIdNumber.IsValid)
        //    {
        //        csvIdPostfix.Validate();
        //        if (csvIdPostfix.IsValid == false)
        //        {
        //            ddlIdPostfix.ControlStyle.CssClass = "validationError";
        //        }
        //        else
        //        {
        //            ddlIdPostfix.ControlStyle.CssClass = "";
        //        }
        //    }
        //}
        //else
        //{
        //    ddlIdPostfix.ControlStyle.CssClass = "";
        //}

        #endregion
    }

    private void GetXmlDatasource()
    {
        try
        {
            //XmlDataSource xdsIdPrefix = new XmlDataSource();
            //xdsIdPrefix.DataFile = "~/controls/IdNumberPrefix.xml";
            //xdsIdPrefix.GetXmlDocument();
            //ddlIdPrefix.DataSource = xdsIdPrefix;
            //ddlIdPrefix.DataTextField = "name";
            //ddlIdPrefix.DataValueField = "value";
            //ddlIdPrefix.DataBind();

            //XmlDataSource xdsIdPostfix = new XmlDataSource();
            //xdsIdPostfix.DataFile = "~/controls/IdNumberPostfix.xml";
            //xdsIdPostfix.GetXmlDocument();
            //ddlIdPostfix.DataSource = xdsIdPostfix;
            //ddlIdPostfix.DataTextField = "name";
            //ddlIdPostfix.DataValueField = "value";
            //ddlIdPostfix.DataBind();

        }
        catch (Exception Ex)
        {

        }
    }

    protected void IdPrefixServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the value is -1
            if (args.Value != "-1")
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    protected void IdPostfixServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the value is -1
            if (args.Value != "-1")
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    //protected void IdNumberLengthServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        // Test whether the lenght of value is < 7.
    //        args.IsValid = !(args.Value.Length < 7);
    //    }

    //    catch (Exception ex)
    //    {
    //        args.IsValid = false;
    //    }
    //}

    protected void CheckIdNumberServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {

            if (args.Value.Length == 13)
            {
                if (args.Value.Length > 0 && args.Value.Length == 13)
                {
                    if (VerifyCitizenID (args.Value))
                    {
                        args.IsValid = true;
                    }
                    else
                    {
                        args.IsValid = false;

                    }
                }
                else
                {
                    args.IsValid = false;
                }
            }
            else
            {
                args.IsValid = false;
            }
        }

        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvIdNumber.Enabled = bValue;
        csvIdNumber.Enabled = bValue;
        revIdNumberLength.Enabled = bValue;
        //csvIdPostfix.Enabled = bValue;
        //csvIdPrefix.Enabled = bValue;
    }

    public string ClientValidationScript
    {
        get { return strClientScript; }
        set { strClientScript = value; }
    }

    public string Prefix
    {
        get { return strIdPrefix; }
        set { strIdPrefix = value; }
    }

    public string Postfix
    {
        get { return strIdPostfix; }
        set { strIdPostfix = value; }
    }

    public string IDNumber
    {
        get { return strIdNumber; }
        set { strIdNumber = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string PrefixErrorMessage
    {
        get { return strPrefixErrorMessage; }
        set { strPrefixErrorMessage = value; }
    }

    public string PostfixErrorMessage
    {
        get { return strPostfixErrorMessage; }
        set { strPostfixErrorMessage = value; }
    }


    public string IdNoErrorMessage
    {
        get { return strIdNoErrorMessage; }
        set { strIdNoErrorMessage = value; }
    }

    public string IdNoRequireErrorMessage
    {
        get { return strIdNoRequireErrrorMessage; }
        set { strIdNoRequireErrrorMessage = value; }
    }

    public string IdNoLengthErrorMessage
    {
        get { return strIdNoLengthErrorMessage; }
        set { strIdNoLengthErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlGender.SelectedIndex = 0;
        //ddlIdPrefix.Enabled = false; //bEnabled;
        //ddlIdPostfix.Enabled = false; //bEnabled;
        txtIdNumber.Enabled = bEnabled;

        //csvIdPrefix.Enabled = false; //bEnabled;
        //csvIdPostfix.Enabled = false; //bEnabled;
        csvIdNumber.Enabled = false; //bEnabled;
        rfvIdNumber.Enabled = bEnabled;
        revIdNumberLength.Enabled = bEnabled;
    }

    private void SetReadOnly()
    {
        //for (int i = 0; i < ddlIdPrefix.Items.Count; i++)
        //{
        //    if (ddlIdPrefix.Items[i].Selected == false)
        //    {
        //        ddlIdPrefix.Items[i].Enabled = !bReadOnly;
        //    }
        //}
        if (bReadOnly)
        {
            //ddlIdPrefix.Text = strIdPrefix;
            //ddlIdPostfix.Text = strIdPostfix;

            //string orgPrefixText = ddlIdPrefix.SelectedItem.Text;
            //string orgPrefixValue = ddlIdPrefix.SelectedItem.Value;

            //string orgPostfixText = ddlIdPostfix.SelectedItem.Text;
            //string orgPostfixValue = ddlIdPostfix.SelectedItem.Value;

            //ddlIdPrefix.Items.Clear();
            //ddlIdPostfix.Items.Clear();
            //ddlIdPrefix.Items.Add(new ListItem(orgPrefixText, orgPrefixValue));
            //ddlIdPostfix.Items.Add(new ListItem(orgPostfixText, orgPostfixValue));

            txtIdNumber.ReadOnly = bReadOnly;
        }
        //for (int i = 0; i < ddlIdPostfix.Items.Count; i++)
        //{
        //    if (ddlIdPostfix.Items[i].Selected == false)
        //    {
        //        ddlIdPostfix.Items[i].Enabled = !bReadOnly;
        //    }
        //}
    }

    protected override void OnPreRender(EventArgs e)
    {
        //SetEnabled();

        //if (strIdPrefix != string.Empty)
        //{
        //    ddlIdPrefix.Text = strIdPrefix;
        //}

        //if (strIdPostfix != string.Empty)
        //{
        //    ddlIdPostfix.Text = strIdPostfix;
        //}

        if (strIdNumber != string.Empty)
        {
            txtIdNumber.Text = strIdNumber;
        }


        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
            {
                //csvIdPrefix.ValidationGroup = strValidatinoGroup;
                //csvIdPostfix.ValidationGroup = strValidatinoGroup;
                revIdNumberLength.ValidationGroup = strValidatinoGroup;
                //csvIdNumber.ValidationGroup = strValidatinoGroup;
                rfvIdNumber.ValidationGroup = strValidatinoGroup;
            }
        }


        if (strPrefixErrorMessage != string.Empty)
        {
            //csvIdPrefix.ErrorMessage = strPrefixErrorMessage;
        }

        if (strPostfixErrorMessage != string.Empty)
        {
            //csvIdPostfix.ErrorMessage = strPostfixErrorMessage;
        }

        if (strIdNoLengthErrorMessage != string.Empty)
        {
            //revIdNumberLength.ErrorMessage = strIdNoLengthErrorMessage;
        }

        if (strIdNoErrorMessage != string.Empty)
        {
            //csvIdNumber.ErrorMessage = strIdNoErrorMessage;
        }

        if (strIdNoRequireErrrorMessage != string.Empty)
        {
            //rfvIdNumber.ErrorMessage = strIdNoRequireErrrorMessage;
        }
    }

    public void SetProperty()
    {
        //strIdPrefix = ddlIdPrefix.SelectedValue;        
        //strIdPostfix = ddlIdPostfix.SelectedValue;
        strIdNumber = txtIdNumber.Text;
    }

    public string GetIdNumber()
    {
        //strIdPrefix = ddlIdPrefix.SelectedValue;
        //strIdNumber = txtIdNumber.Text;
        //strIdPostfix = ddlIdPostfix.SelectedValue;
        //string _idno = strIdPrefix + strIdNumber + strIdPostfix;

        string _idno = txtIdNumber.Text;
        return _idno;
    }

    public string GetIdPrefix()
    {
        return string.Empty; //ddlIdPrefix.Text;       
    }

    public bool IsValidePrefixRequirement()
    {
        //csvIdPrefix.Validate();
        //return csvIdPrefix.IsValid;
        return true;
    }

    private bool VerifyCitizenID(string citizenID)
    {
        //check isnumeric  
        //if (citizenID.ToCharArray().All(c => char.IsNumber(c)) == false)
        //    return false;
                
        //13 digit
        if (citizenID.Trim().Length != 13)
            return false;
        
        int sumValue = 0;        

        for (int i = 0; i < citizenID.Length - 1; i++)
            sumValue += int.Parse(citizenID[i].ToString()) * (13 - i);

        int v = 11 - (sumValue % 11);
        
        return citizenID[12].ToString() == v.ToString();
    }
}